package com.shop2cn.iapi.couponmanage.testcase;

import java.util.Date;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.couponmanage.parameter.CreateBagBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.InvalidBatchBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.service.CreateBagCall;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.InvalidBatchCall;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
//import com.ymttest.database.model.couponbatch;
//import com.ymttest.database.sqlwapper.couponbatchWapper;

import com.ymttest.database.model.CouponShop2cn.couponbatch;
import com.ymttest.database.model.CouponShop2cn.couponbatchext;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponbatchWapper;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponbatchextWapper;

import oracle.net.aso.n;
/**
*作废优惠券批次信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_InvalidBatch{
	private static InvalidBatchBean invalidbatchBean;
	private static InvalidBatchCall invalidbatchCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("作废优惠券批次信息"); 
	}

	@Before
	public void caseUp() {
		invalidbatchBean=new InvalidBatchBean();
		invalidbatchCall=new InvalidBatchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(invalidbatchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_001() {
		Logger.start(true,"作废优惠券批次信息-作废商家批次");
		try {
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponbatch batch = new couponbatchWapper().selectBybatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(batch.getOperatorId());
			invalidbatchBean.setBatchCode(batch.getBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
			Logger.verifyEquals("0", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", invalidbatchCall.getMessage(), "验证返回Message");
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_009() {
		Logger.start(true,"作废优惠券批次信息-作废已作废商家批次");
		try {
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponbatch batch = new couponbatchWapper().selectBybatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(batch.getOperatorId());
			invalidbatchBean.setBatchCode(batch.getBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();		
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();	
			
			Logger.verifyEquals("0", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", invalidbatchCall.getMessage(), "验证返回Message");
			verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_010() {
		Logger.start(true,"作废优惠券批次信息-作废商家批次-sellerid与batchid不匹配");
		try {
			couponbatch batch  = new couponbatchWapper().getTop1SellerBatchInfo().get(0);
	
			invalidbatchBean.setSellerId(2222);
			invalidbatchBean.setBatchCode(batch.getBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
//			Logger.verifyEquals("2", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyIsContains("您没有权限作废此批次，请勿非法操作", invalidbatchCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_011() {
		Logger.start(true,"作废优惠券批次信息-作废商家批次-batchid不存在");
		try {
			couponbatch batch = new couponbatchWapper().getTop1SellerBatchInfo().get(0);
	
			invalidbatchBean.setSellerId(Integer.parseInt(batch.getOperatorId().toString()));
			invalidbatchBean.setBatchCode("747257");
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
//			Logger.verifyEquals("2", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次不存在", invalidbatchCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_InvalidBatch_0012() {
		Logger.start(true,"作废优惠券批次信息-作废商家批次-买手id 为空");
		try {
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			SellerCreateBatchBean sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponbatch batch = new couponbatchWapper().selectBybatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
			
			invalidbatchBean.setSellerId(0);
			invalidbatchBean.setBatchCode(batch.getBatchCode());
			
			invalidbatchCall.setData(invalidbatchBean);
			invalidbatchCall.callService();			
			
			Logger.verifyEquals("1", invalidbatchCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, invalidbatchCall.getMessage().contains("请传入商家编号"), "验证返回Message");
			//verifyDB();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	private void verifyDB() {
		couponbatch couponBatch = new couponbatchWapper().selectBybatchCode(invalidbatchBean.getBatchCode())
				.get(0);
		couponbatchext couponbatchext =new couponbatchextWapper().selectByPrimaryKey(couponBatch.getBatchId());

		Logger.verifyEquals(true, couponBatch.getIsInvalid(), "验证数据库中couponbatch表中IsInvalid字段正确");
		Logger.verifyEquals(true, checkDateNew(couponbatchext.getInvalidTime()),
				"验证数据库中couponbatchext表中dInvalidTime字段正确");

		if (invalidbatchBean.getInvalidUser() != null) {
			Logger.verifyEquals(invalidbatchBean.getInvalidUser(), couponbatchext.getInvalidUser(),
					"验证数据库中couponbatchext表中InvalidUser字段正确");
		}

		if (invalidbatchBean.getInvalidMemo() != null) {
			Logger.verifyEquals(invalidbatchBean.getInvalidMemo(), couponbatchext.getInvalidMemo(),
					"验证数据库中couponbatchext表中InvalidMemo字段正确");
		}

	}
	
	 private boolean checkDateNew(Date date)
	    {
	        Date now = new Date();
	        if (date.getYear() != now.getYear() && date.getMonth() != now.getMonth() && date.getDate() != now.getDate()
	                && date.getHours() != now.getHours() && Math.abs(date.getMinutes() - now.getMinutes()) > 3)
	        {
	            return false;
	        }
	        return true;
	    }
}